package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.DbFileDAO;
import org.millioncall.yueyoga.admin.model.DbFile;
import org.springframework.stereotype.Repository;

@Repository
public class DbFileDAOImpl extends BaseDao implements DbFileDAO {

	@Override
	public void save(DbFile file) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", file.getId());
			map.put("type", file.getType());
			map.put("fileName", file.getFileName());
			map.put("lastModified", file.getLastModified());
			map.put("length", file.getLastModified());
			map.put("content", file.getContent());
			this.getPagableSqlSessionTemplate().insert("insertDbFile", map);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void update(DbFile file) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", "id");
			map.put("value", file.getId());
			this.getPagableSqlSessionTemplate().update("updateDbFile", map);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void delete(DbFile file) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", "id");
			map.put("value", file.getId());
			this.getPagableSqlSessionTemplate().delete("deleteDbFile", map);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public int deleteByName(String name) {
		int i = 0;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", "name");
			map.put("value", name);
			this.getPagableSqlSessionTemplate().delete("deleteByName", map);
		} catch (Exception e) {
			throw e;
		}
		return i;
	}

	@Override
	public int deleteById(String id) {
		int i = 0;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", "id");
			map.put("value", id);
			this.getPagableSqlSessionTemplate().delete("deleteById", map);
		} catch (Exception e) {
			throw e;
		}
		return i;
	}

	@Override
	public int deleteByIds(String[] ids) {
		int i = 0;
		for(String Id : ids){
			i = this.deleteById(Id);
		}
		return i;
	}

	@Override
	public DbFile findById(String id) {
		DbFile dbFile = null;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", "id");
			map.put("value", id);
			dbFile = this.getPagableSqlSessionTemplate().selectOne("findById", map);
		} catch (Exception e) {
			throw e;
		}
		return dbFile;
	}

	@Override
	public DbFile findByName(String filename) {
		DbFile dbFile = null;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", "fileName");
			map.put("value", filename);
			dbFile = this.getPagableSqlSessionTemplate().selectOne("findByName", map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dbFile;
	}

	@Override
	public List<DbFile> findAllByType(String type) {
		List<DbFile> list = null;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", "type");
			map.put("value", type);
			list = this.getPagableSqlSessionTemplate().selectList("findByType", map);
		} catch (Exception e) {
			throw e;
		}
		return list;
	}

	@Override
	public List<DbFile> findAll() {
		List<DbFile> list = null;
		try {
			list = this.getPagableSqlSessionTemplate().selectList("findAll");
		} catch (Exception e) {
			throw e;
		}
		return list;
	}

}
